"""
vas 系统数据API格式
"""
import json
from http.server import BaseHTTPRequestHandler, HTTPServer

zookeeper_data = {
    # 视频源配置
    'channel': {
        "channel": "channel_001",  # 视频源ID
        "url": "rtsp://admin:pwd@129.7.4.8/channel",  # 视频源地址
        "tasks": [{  # 视频源分析任务列表
            "type": "HELMET-DETECTION",  # 任务名
            "roi": [],  # 分析区域
            "interval": 10  # 报警间隔
        }, {
            "type": "AREA_DETECTION",
            "roi": [[10, 20], [30, 40]],
            "interval": 10
        }]
    },
}

rocket_mq_data = {
    # 分析结果
    'result': {
        "channel": "channel_001",  # 视频源ID
        "image": "base64 encrypted image",  # Base64 序列化的图片
        "timeStamp": 1599639857,  # 图片帧时间戳
        "tasks": [{  # 视频源分析任务列表
            "type": "HELMET_DETECTION",  # 任务名
            "result": [{  # 分析结果列表
                "label": "OK",  # 标签
                "coordinate": [0, 0, 1280, 720]  # 标记框
            }, {
                "label": "NG",
                "coordinate": [10, 20, 1370, 280]
            }]
        }, {
            "type": "AREA_DETECTION",
            "result": [{
                "label": "OK",
                "coordinate": [0, 0, 1280, 720]
            }]
        }]
    },
}

http_get_data = {
    # 查看当前接入的数据源 GET /sources
    '/sources': [
        {
            "channel": "channel1",  # 视频源ID
            "url": "rtsp://admin:pwd@192.168.1.5/channel1",  # 视频源地址
            "tasks": [{  # 视频源分析任务列表
                "type": "HELMET-DETECTION",  # 任务名
                "roi": [],  # 分析区域
                "interval": 10  # 报警间隔
            }, {
                "type": "AREA-DETECTION",
                "roi": [[10, 20], [30, 40]],
                "interval": 10
            }]
        }
    ],
    # 查看计算图情况 GET /graph
    '/graph': [
        {
            "name": "Route",
            "tc": 23,  # 时间消耗 (ms)
            "tps": 5,  # Transaction per second
            "elh": 0,  # Error number in latest hundred commits
            "to": ["PedAttrRecognition", "RocketMsgSender"]  # Nodes followed
        }, {
            "name": "PedAttrRecognition",
            "tc": 23,
            "tps": 5,
            "elh": 0,
            "to": []
        }, {
            "name": "RocketMsgSender",
            "tc": 23,
            "tps": 5,
            "elh": 0,
            "to": []
        }
    ],
    # 查看功能统计 GET /task/delay
    '/task/delay': [
        {
            "type": "HELMET-DETECTION",
            "delay": 235  # 时间消耗 (ms)
        }
    ],
    # 查看当前缓存内容（不会显示具体数据） GET /cache
    '/cache': [
        {
            "ns": "namespace1",  # 命名空间
            "group": [  # 分组列表
                {
                    "gp": "group1",  # 分组名
                    "data": [  # 数据列表
                        {
                            "key": "key1",  # 数据键
                            "versions": [8, 7, 6, 5, 4],  # 数据最新版本号
                            "ctime": 10,  # 数据创建时间
                            "mtime": 18,  # 最后更新时间
                            "cache-size": 5,  # 缓存容量
                        }
                    ]
                }
            ]
        }
    ],
    # 根据namespace group key 查询具体缓存最新版本内容 GET /cache/namespace1/group1/key1
    '/cache/namespace1/group1/key1': {
        "version": 8,  # 版本号
        "ctime": 10,  # 创建时间
        "mtime": 18,  # 更新时间
        "body": "..."  # 数据值
    },
    # 根据namespace group key 版本号 查询具体缓存内容 GET /cache/namespace1/group1/key1/4
    '/cache/namespace1/group1/key1/4': {
        "version": 4,
        "ctime": 10,
        "mtime": 15,
        "body": "..."
    }
}

# 配置模型
config = {
    "source": {
        "type": "local",  # local or zookeeper
        "max": 5,  # max num of sources analysis concurrently
        "host": "192.168.1.1:2171",  # zk host, necessary if type is zookeeper
        "channels": [  # video source urls, necessary if type is local
            {
                "channel": "channel_001",  # 视频源ID
                "url": "rtsp://admin:pwd@129.7.4.8/channel",  # 视频源地址
                "tasks": [{  # 视频源分析任务列表
                    "type": "HELMET-DETECTION",  # 任务名
                    "roi": [],  # 分析区域
                    "interval": 10  # 报警间隔
                }, {
                    "type": "AREA_DETECTION",
                    "roi": [[10, 20], [30, 40]],
                    "interval": 10
                }]
            },
        ]
    },
    "algorithms": [{
        "name": "PedAttrRecognition",  # algorithm name
        "mock": 0  # 0 or 1, 1 = start the mock stump instead of real algorithm model
    }, {
        "name": "HumanDetection",
        "mock": 0
    }],
    "http": {
        "port": 8077,  # http server port
        "thread": 16  # http server thread num
    }
}

# 计算数据
compute_data = {
    "Header": {
        "Id": "...uuid...",
        "Content-Type": "Normal",  # Normal, Probe, Instruction
        "Compute-Type": "Dynamic",  # Dynamic, Static
        "Route": ["HumanDetection", "PedAttrRecognition", "RocketMsgSender"],
        "Trace": ["HumanDetection", "PedAttrRecognition"]
    },
    "Body": "..."  # Data for compute in the graph
}

# 缓存数据
cache_data = {
    "Header": {
        "Namespace": "namespace1",  # namespace name
        "Group": "group1",  # group name
        "Key": "key1",  # data key
        "Ctime": 10,  # created timestamp
        "Mtime": 18,  # last modified timestamp
        "Version": 4,  # data version
        "Keep-Size": 5  # keep alive version size
    },
    "Body": "..."  # data value
}

# 算法计算数据
algorithm_data = {
    "channel": "channel_001",  # 视频源ID
    "image": [],  # OpenCV Mat格式图片数据
    "timeStamp": 1599639857,  # 图片帧时间戳
    "scene-labels": ['no_fire'],  # 场景识别结果
    "pedestrians": [
        {
            "id": '1',  # 行人ID
            "box": [[10, 20], [40, 50]],  # 行人检测框
            "attributes": ['helmet', 'short', 'glass'],  # 行人属性
            "key-points": [[10, 20], [40, 50]],  # 行人关键点
            "actions": ['walk', 'run'],  # 行人行为
            "traces": [[10, 20], [40, 50]],  # 行人轨迹
            "restricted-area": True,  # 行人是否处在禁区位置
        }
    ]
}


class MyHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        path = str(self.path)
        if path in http_get_data.keys():
            self.send_response(200)
            self.end_headers()
            res = json.dumps(http_get_data[path]).encode('utf8')
            self.wfile.write(res)
        else:
            self.send_response(404)
            self.end_headers()


def start_http(port):
    server = HTTPServer(('0.0.0.0', port), MyHandler)
    server.serve_forever()


if __name__ == '__main__':
    start_http(50001)
